<?php
    include "database.php";
    //Za delovanje seje na vseh nivojih
    session_set_cookie_params(0,"/");
    session_start();

    if(!isset($_SESSION['username'])) {
        header("Location: login.php");
    }
    
    $admin = checkAdmin($_SESSION['id']);
    $id = $_SESSION['id'];
?>
<!DOCTYPE html>

<html>
	<head>
	    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		
		<!-- Font weight: light (300) & semibold (600) -->
		<link href='http://fonts.googleapis.com/css?family=Open+Sans:600,300' rel='stylesheet' type='text/css'>
		<link rel="stylesheet" href="css/default.css"/>
		<link rel="stylesheet" href="css/tabs.css"/>
		<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.2/themes/smoothness/jquery-ui.css" />
		
		<script type="text/javascript" src="js/jquery-1.9.1.min.js"></script>
		<script type="text/javascript" src="js/jquery-ui-1.10.2.custom.min.js"></script>
		<script type="text/javascript" src="js/actions.js"></script>
		
		<title>Scrum | Project Wall</title>		
	</head>
	
	<body>
	
            <span id="username"><?=getUsername($id)?></span>
		<span id="separator">|</span>
		<span id="role"><a href="edit_profile.php">edit</a></span>
		
		<h1>Scrum</h1>
		
		<menu>
			<li><a href="control_panel.php">control<span>Panel</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a class="active" href="project_wall.php">project<span>Wall</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="product_backlog.php">product<span>Backlog</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="sprint_backlog.php">sprint<span>Backlog</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="my_tasks.php">my<span>Tasks</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="progress_report.php">progress<span>Reports</span></a>&nbsp;&nbsp;|&nbsp;</li>
			<li><a href="login.php">logout</a></li>
		</menu>
		
		<section>
		
			<ul id="tabs">
				<li><h2><a href="#" name="discussion">project<span>Discussion</span></a></h2></li>
				<li><h2><a href="#" name="documentation">project<span>Documentation</span></a></h2></li>
			</ul>
			
			<!--=========-->
			<!-- Dialogs -->
			<!--=========-->
			
			<div id="delete_post_dialog" title="Delete post">
				<p>Are you sure you want to delete this post?</p>
			</div>
                        
                        <div id="delete_comment_dialog" title="Delete comment">
				<p>Are you sure you want to delete this comment?</p>
			</div>
                        
                        <div id="export_dialog" title="Documentation export">
				<p>Download file by clicking on the link below.</p>
                                <br/>
                                <!--<p><a id="download" style="visibility:hidden; text-decoration:underline;" href="http://localhost/TpoProject/php/Documentation.txt" download="Documentation.txt">Save file to your computer</a></p>-->
                                <p><a id="download" style="visibility:hidden; text-decoration:underline;" href="http://ltpo2.fri1.uni-lj.si/vaje2013/TPO15/php/Documentation.txt" download="Documentation.txt">Save file to your computer</a></p>
                        </div>
			
			<div id="tab_content">
			
				<!--============-->
				<!-- Discussion -->
				<!--============-->
			
				<div id="discussion" class="stories_container">
					<div class="story">
						<form method="post" action="./php/postToWall.php">
                                                        
							<textarea class="new_post" name="new_post" placeholder="Post a new message..."></textarea>
							<div class="documentation_buttons">
								<button type="submit" class="documentation_button post_comment">post to wall</button>
							</div>
						</form>
					</div>
                                                                                          
					<!-- WALL POSTS -->
                                        <?php //PRINT ALL POSTS ON WALL
                                        $query = "SELECT current_project FROM user WHERE id = '$id'";
                                        $project_id = mysql_fetch_array(executeQuery($query))[0];
                                        $query = "SELECT * FROM wall_posts WHERE project_id = '$project_id' ORDER BY timestamp DESC LIMIT 50";
                                        $result = executeQuery($query);
                                        while($row = mysql_fetch_array($result)){
                                            $content = $row['content'];
                                            $timestamp = $row['timestamp'];
                                            $author_id = $row['author_id'];
                                            $post_id = $row['post_id'];
                                            $username = getUsername($author_id);
                                            

                                        ?>
                                        
                                            <div class="story">
                                                    <input type="hidden" name="post_id" value=<?=$post_id?> />

                                                    <div class="header">
                                                            <h5><?=$username?></h5><br/>
                                                            <?php
                                                            //Lahko brisemo post samo če smo scrum master ali lastnik 
                                                            if(isScrumMaster($id, $project_id) || $id == $author_id){?>
                                                                <span class="values all_stories"><a class="delete_post">delete</a></span>
                                                            <?php } ?>
                                                            <?=date("d. m. Y, H:i:s", strtotime($timestamp))?>
                                                    </div>
                                                    
                                                    <?= $content?>
                                                    <br/>
                                                    <br/>
                                                    
                                                    <?php
                                                    //--------------KOMENTARJI
                                                    $queryComment = "SELECT * FROM commentary WHERE commentary_post_id = '$post_id'";
                                                    $resultComment = executeQuery($queryComment);
                                                    while($rowComment = mysql_fetch_array($resultComment)){
                                                        $commentId = $rowComment['commentary_id'];
                                                        $contentComment = $rowComment['commentary_text'];
                                                        $timestampComment = $rowComment['commentary_date'];
                                                        $comment_author_id = $rowComment['commentary_user_id'];
                                                        $usernameComment = getUsername($comment_author_id);
                                                    
                                                    print "<div class=\"header\">
                                                                <h5>$usernameComment</h5><br/>";
                                                           if(isScrumMaster($id, $project_id) || $id == $comment_author_id){
                                                               print "<input type=\"hidden\" name=\"comment_id_delete\" value=\"$commentId\" />
                                                                      <span class=\"values all_stories\"><a class=\"delete_comment\">delete</a></span>"; 
                                                           }
                                                           echo date("d. m. Y, H:i:s", strtotime($timestampComment));
                                                           echo "</div>";
                                                           print "<p>$contentComment</p>";
                                                           echo "<br/>";
       
                                                    }
                                                    ?>
                                                    
                                                    <!-- IZPIS KOMENTARJEV JE V MAPI html -->
                                                    
                                                    <div class="header">
                                                            <h5>Post new comment</h5>
                                                    </div>
                                                    <form method="post" action="./php/addCommentary.php">
                                                            <input type="hidden" name="post_id" value=<?=$post_id?> />
                                                            <textarea class="comment" name="comment" placeholder="comment..."></textarea>
                                                            <div class="documentation_buttons">
                                                                    <button type="submit" class="documentation_button post_comment">post</button>
                                                            </div>
                                                    </form>

                                            </div>
                                            
                                            <?php } ?>
                                        
                                            
                                        
					
				</div>
				
				<!--===============-->
				<!-- Documentation -->
				<!--===============-->
				
				<div id="documentation" class="stories_container">
                                    
                                    <?php //PRINT ALL POSTS ON WALL
                                        $queryProject = "SELECT current_project FROM user WHERE id = '$id'";
                                        $project_id = mysql_fetch_array(executeQuery($queryProject))[0];
                                        
                                        $queryDoc = "SELECT documentation_content FROM documentation WHERE documentation_project_id = '$project_id'";
                                        $doc_content = mysql_fetch_array(executeQuery($queryDoc))[0];
                                        ?>
                                    
                                    
					<div class="story">
						<form method="post" id="doc_form" action="./php/editDocumentation.php" enctype="multipart/form-data">                                                        
                                                        <input type="hidden" name="doc_project_id" value="<?=$project_id?>" />
							<textarea id="documentation_textarea" name="documentation" placeholder="add documentation..."><?=$doc_content?></textarea>
							<div id="doc_buttons" class="documentation_buttons">
                                                            <button type="submit" class="documentation_button submit_documentation">save changes</button>
                                                            <button type="button" id="export" class="documentation_button export_documentation gray">export to .txt</button>
							</div>
                                                </form>
                                            
                                                <form method="post" id="import_form" action="./php/documentationImport.php" enctype="multipart/form-data">
                                                    <input type="hidden" name="doc_project_id" value="<?=$project_id?>" />
                                                    <input id="file" type="file" name="file"></input>
                                                    <button type="submit" id="import" class="documentation_button import_documentation gray">import from .txt</button>
                                                </form>                                  
					</div>                                                                              
				</div>
			</div>
		
		</section>
		
	</body>
</html>

